package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import algorithm.Dynamic_credit;
import algorithm.Point_record;
import DB.DB_connection_pool;

public class Attention extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		String dynamic_information_id = request.getParameter("dynamic_information_id");   //获取动态id
		
		//System.out.println(dynamic_information_id+"sdasd");
		
		String user_account = request.getSession().getAttribute("user_account").toString();   //用户账号
		String passive_user_account = "";    //被关注者账号
		JSONObject jsonObj = new JSONObject();
		String passive_user_select = "select * from dynamic_information where dynamic_information_id = '"+dynamic_information_id+"'";
		String attention_number = "update dynamic_information set dynamic_information_attention_number = dynamic_information_attention_number+1 where dynamic_information_id = '"+dynamic_information_id+"'";
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select = conn.prepareStatement(passive_user_select);
			ResultSet rs = ps_select.executeQuery();
			
			if(rs.next()){
			 passive_user_account = rs.getString("user_account");
			 
			 
			 //用于判断是否关注了这个人
			 String select_attention = "select * from attention where attention_user_id = '"+user_account+"' and passive_attention_user_id = '"+passive_user_account+"'";
			 PreparedStatement ps_at_select = conn.prepareStatement(select_attention);
			 ResultSet rs_at = ps_at_select.executeQuery();
			 
			 if(!rs_at.next()){
			 //关注人
			 String insert_sql = "insert into attention(attention_user_id,passive_attention_user_id) values(?,?)";
				PreparedStatement ps_insert = conn.prepareStatement(insert_sql);
				ps_insert.setString(1, user_account);
				ps_insert.setString(2, passive_user_account);
				ps_insert.executeUpdate();
				
				ps_insert.close();
			}
			 ps_at_select.close();
			 rs_at.close();
			 
			 
			 //用于判断是否已经关注了这个动态
			 String select_attention_dy = "select * from attention_dynamic where attention_user_id = '"+user_account+"' and dynamic_information_id = '"+dynamic_information_id+"'";
			 PreparedStatement ps_select_dy = conn.prepareStatement(select_attention_dy);
			 ResultSet rs_dy = ps_select_dy.executeQuery();
			 if(!rs_dy.next()){         //还没有关注
				 String insert_at_dy = "insert attention_dynamic(attention_user_id,dynamic_information_id) values(?,?)";
				 PreparedStatement ps_insert_dy = conn.prepareStatement(insert_at_dy);
				 ps_insert_dy.setString(1, user_account);
				 ps_insert_dy.setString(2, dynamic_information_id);
				 ps_insert_dy.executeUpdate();
				 ps_insert_dy.close();
				 
			 //关注+1
				PreparedStatement attention_up = conn.prepareStatement(attention_number);
				attention_up.executeUpdate();
				attention_up.close();
				
				jsonObj.put("attentioned", true);
			}else{
				jsonObj.put("attentioned", false);
			}
			
			 jsonObj.put("result", true);
			 
//////////////////--------积分相关，由于之前没设计好，导致这一块代码耦合程度低，再次特别标出------///////////
			    Dynamic_credit dc = new Dynamic_credit();
				Map<String, Integer> map = dc.Get_credit(Dynamic_credit.attention);

				Point_record pr = new Point_record();
				if(map.get("flag")==1){            //积分改变
				pr.change_point(user_account,(double)(map.get("credit")));
				pr.point_record(user_account, Point_record.attention_event, (double)(map.get("credit")), true);
				}
				pr.point_being_change(dynamic_information_id, Point_record.being_attention_event);
	///////////////---------end积分--------////////////////
			}else{
				jsonObj.put("result", false);
			}
			conn.close();
			ps_select.close();
			rs.close();
			
			
		} catch (SQLException e) {
			jsonObj.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		out.print(jsonObj.toString());
		
	}

}
